<template>
  <!-- 筛选区 -->
  <div class="sub-filter" v-if='filterParams && !filterLoading'>
    <!-- 品牌筛选条件 -->
    <div class="item">
      <div class="head">品牌：</div>
      <div class="body">
        <a @click="filterParams.brands.selectedBrand=item.id" :class='{active: filterParams.brands.selectedBrand===item.id}' href="javascript:;" v-for="item in filterParams.brands.list" :key="item.id">{{item.name}}</a>
      </div>
    </div>
    <!-- 其他筛选条件 -->
    <div class="item" v-for="item in filterParams.saleProperties" :key="item.id">
      <div class="head">{{item.name}}：</div>
      <div class="body">
        <a @click="item.selectedProp=tag.id" :class='{active: item.selectedProp===tag.id}' href="javascript:;" v-for="tag in item.properties" :key="tag.id">{{tag.name}}</a>
      </div>
    </div>
  </div>
  <!-- 如果没有数据就显示骨架屏 -->
  <div v-else class="sub-filter">
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="800px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
    <XtxSkeleton class="item" width="600px" height="40px" />
  </div>
</template>
<script>
import { findSubCategoryFilter } from '@/api/category'
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
export default {
  name: 'SubFilter',
  emits: ['filter-change'],
  setup (props, { emit }) {
    const route = useRoute()
    const filterParams = ref(null)

    // 筛选条件数据加载的状态位
    const filterLoading = ref(false)

    // 侦听路由变化
    watch(() => route.params.id, (newId) => {
      if (route.fullPath === '/category/sub/' + newId) {
        filterLoading.value = true
        findSubCategoryFilter(newId).then(res => {
          if (res.result.brands) {
            // 添加选中的属性，用于记录选中的条件值
            // res.result.brands.selectedBrand = null
            res.result.brands.unshift({
              id: null,
              name: '全部'
            })
          }

          if (res.result.saleProperties) {
            res.result.saleProperties.forEach(item => {
              // 添加选中的属性，用于记录选中的条件值
              item.selectedProp = null
              item.properties.unshift({
                id: null,
                name: '全部'
              })
            })
          }

          filterParams.value = {
            brands: {
              list: res.result.brands,
              selectedBrand: null
            },
            saleProperties: res.result.saleProperties
          }

          filterLoading.value = false
        })
      }
    }, { immediate: true })

    // 侦听筛选条件变化
    watch(() => filterParams, (newVal) => {
      // 根据接口文档整理数据
      const attrs = []

      // 遍历变化后的值
      newVal.value.saleProperties && newVal.value.saleProperties.forEach(item => {
        // key值根据接口文档
        const param = {
          // 组名
          groupName: item.name,
          // 组里面单个选项名,根据id找出对应的项,取出name属性并赋值,如果没选赋值null
          propertyName: item.selectedProp ? item.properties.find(tag => tag.id === item.selectedProp).name : null
        }
        attrs.push(param)
      })

      // 整理好数据之后传给父亲
      emit('filter-change', {
        brandId: filterParams.value.brands.selectedBrand,
        arttrs: attrs
      })
    }, { deep: true })

    return { filterParams, filterLoading }
  }
}
</script>

<style scoped lang='less'>
  // 筛选区
  .sub-filter {
    background: #fff;
    padding: 25px;
    .xtx-skeleton {
    padding: 10px 0;
     }
    .item {
      display: flex;
      line-height: 40px;
      .head {
        width: 80px;
        color: #999;
      }
      .body {
        flex: 1;
        a {
          margin-right: 36px;
          transition: all .3s;
          display: inline-block;
          &.active,
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
</style>
